"ui";
importClass(org.opencv.imgcodecs.Imgcodecs);//导入opencv的包
const tool = require('./tool.js');
const spyl = require('./spyl.js');
const xhsyl = require('./xhsyl.js');
const ttyy = require('./ttyy.js');
const ksyl = require('./ksyl.js');
const car = require('./car.js');
ui.layout(
    <vertical h="4000">
        <horizontal>
            <text text="搜索词" />
            <input id="searchWord" w="200" text="食品" />
        </horizontal>
        <horizontal>
            <text text="评论词" />
            <input id="pinglun" text="求中福袋，求中福袋，点我主页有惊喜" />
        </horizontal>
        <horizontal>
            <Switch id="isOpenConsole" text="控制台" gravity="center" w="100" h="20" />
        </horizontal>
        <horizontal>
            <text text="卡密" />
            <input id="keygen" w="200" text="" />
            <button id="saveParam" text="保存" />
        </horizontal>
        <horizontal>
            <button id="spyl" text="抖音引流" />
            <button id="xhsyl" text="小红书引流" />
            <button id="ksyl" text="快手引流" />
            <button id="ttyy" text="tt语音" />
        </horizontal>
        <horizontal>

        </horizontal>
    </vertical>
);


//快手引流
ui.ksyl.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        tool.init(params)
        ksyl.main(params)
    });
});

//小红书引流
ui.xhsyl.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        tool.init(params)
        xhsyl.main(params)
    });
});



//抖音引流
ui.spyl.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        tool.init(params)
        spyl.main(params)
    });
});


//tt语音
ui.ttyy.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        ttyy.main(params)
    });
});

//保存
ui.saveParam.click(function () {
    //保存参数
    let params = collectParameters()
    //效验卡密
    car.updateCardInfo(params)
})


//收集界面配置参数
function collectParameters() {
    var params = {}
    //搜索词
    params.searchWord = ui.searchWord.text()
    //评论
    params.pinglun = ui.pinglun.text()
    //控制台开关
    params.isOpenConsole = ui.isOpenConsole.checked;
    //卡密
    params.keygen = ui.keygen.text()

    return params
}

//初始化配置数据
function initParameters() {
    try {
        //读取配置文件
        let params = car.readConfig()
        console.log("读取参数:" + JSON.stringify(params))
        if (params.searchWord !== "" && params.searchWord !== undefined && params.searchWord !== null) {
            ui.searchWord.text(params.searchWord)
        }
        if (params.pinglun !== "" && params.pinglun !== undefined && params.pinglun !== null) {
            ui.pinglun.text(params.pinglun)
        }

        if (params.isOpenConsole !== "" && params.isOpenConsole !== undefined && params.isOpenConsole !== null) {
            ui.isOpenConsole.setChecked(params.isOpenConsole)
        }
          //卡密
          if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }

        return params
    } catch (e) {
        console.log("读取参数异常:" + e)
    }
}

//初始化配置数据
initParameters()